import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
// import Login from '../views/Login'
import Layout from '../views/Layout'
import Home from '../views/Home'
// import User from '../views/User'
// import Search from '../views/Search'
// import ArticleDetail from '../views/ArticleDetail'
// import SearchResult from '../views/SearchResult'
// import UserEdit from '../views/User/UserEdit.vue'
// import Chat from '../views/Chat'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/layout',
    component: Layout,
    redirect: '/layout/home',
    children: [{
      path: 'home',
      component: Home
    },
    {
      path: 'user',
      component: () => import('../views/User')
    }]
  },
  {
    path: '/search',
    component: () => import('../views/Search')
  },
  {
    path: '/search/:keywords',
    component: () => import('../views/SearchResult')
  },
  {
    path: '/article/:id',
    component: () => import('../views/ArticleDetail')
  },
  {
    path: '/user/edit',
    component: () => import('../views/User/UserEdit.vue')
  },
  {
    path: '/chat',
    component: () => import('../views/Chat')
  }
]
const router = new VueRouter({
  routes
})
// 路由全局前置守卫
router.beforeEach((to, from, next) => {
  // 如果登录了,就不能重新登录页面
  // 在跳转登录页面之前,进行判断是否有token值,并且跳转的页面是否为登录页面
  if (store.state.token.length > 0 && to.path === '/login') {
    next(false)
  } else {
    next()
  }
})
export default router
